#![allow(dead_code)]

pub struct VecLrn {}

impl VecLrn {
    pub fn hello() {
        println!("hello");
    }
}

#[allow(unused_imports)]
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn array_adjust() {
        let mut arr = [1, 2, 3, 4, 5, 6];
        arr[0] = 10;
        assert_eq!(arr, [10, 2, 3, 4, 5, 6]);
    }

    #[test]
    fn vec_adjust() {
        let mut vec = vec![1, 2, 3, 4, 5, 6];
        vec[0] = 10;
        assert_eq!(vec, [10, 2, 3, 4, 5, 6]);
    }
    #[test]
    /// append() 消耗&mut vec
    fn vec_append() {
        let mut vec1 = vec![1, 2, 3];
        let mut vec2 = vec![4, 5, 6];
        vec1.append(&mut vec2);
        assert_eq!(vec1, [1, 2, 3, 4, 5, 6]);
        assert_eq!(vec2, []);
    }

    #[test]
    /// drain() 取范围,返回的是切片
    fn vec_drain() {
        let mut v = vec![1, 2, 3];
        let u: Vec<_> = v.drain(1..).collect();
        assert_eq!(v, &[1]);
        assert_eq!(u, &[2, 3]);

        // 全范围清除 vector，就像 `clear()` 一样
        v.drain(..);
        assert_eq!(v, &[]);
    }

    #[test]
    /// split_off() 索引,返回vec
    fn vec_split_off() {
        let mut vec = vec![1, 2, 3];
        let vec2 = vec.split_off(1);
        assert_eq!(vec, [1]);
        assert_eq!(vec2, [2, 3]);
    }

    #[test]
    fn test_as_mut_ptr() {
        let size = 4;
        // let mut x: Vec<i32>;
        let mut x: Vec<i32> = Vec::with_capacity(size);
        let x_ptr = x.as_mut_ptr();

        unsafe {
            for i in 0..size {
                *x_ptr.add(i) = i as i32 + 1;
            }
            // count为偏移量
            *x_ptr.add(0) = 5;
            // 之前指针没有长度
            x.set_len(size);
        }

        assert_eq!(&*x, &[5, 2, 3, 4]);
    }

    #[test]
    fn remove_and_swap_remove() {
        {
            let mut v = vec![1, 2, 3, 4, 5, 6];
            assert_eq!(v.remove(0), 1);
            assert_eq!(v, [2, 3, 4, 5, 6]);
        }
        {
            let mut v = vec![1, 2, 3, 4, 5, 6];
            assert_eq!(v.swap_remove(0), 1);
            assert_eq!(v, [6, 2, 3, 4, 5]);
        }
    }
}
